<template>
	<view>
		<view class="uni-flex-column content">
			<view id="tab-bar" class="uni-swiper-tab" scroll-x :scroll-left="scrollLeft">
				<view
					v-for="(tab, index) in tabBars"
					:key="tab.id"
					class="swiper-tab-list"
					:class="tabIndex == index ? 'active' : ''"
					:id="tab.id"
					:data-current="index"
					@click="tapTab"
				>{{ tab.name }}
				</view>
			</view>
			<uni-search-bar
				class="search-input" 
				radius="10" 
				placeholder="输入标题" 
				clearButton="always" 
				cancelButton="auto"
				@confirm="search1" 
				@cancel="search2"
			/>
			<swiper :current="tabIndex" class="swiper-box" :duration="300" @change="changeTab">
				<template v-for="(tab, index) in tabBars">
					<swiper-item class="uni-flex-column">
						<view class="list" style="height: 100%;">
							<expand-list ref="expandlist">
								<template slot-scope="{ item, index }">
									<uni-list-item class="list-item list-box" :showArrow="false" :key="index" @click="toInfo(item, index)"> 
										<view class="list-title-box">
											<view class="list-text">
												<p class="list-text-title">{{item.subject}}</p>
												<p class="list-text-title1">当前节点:&nbsp;&nbsp;{{item.name}}</p>
												<p class="list-text-title1">创建时间:&nbsp;&nbsp;{{item.createTime}}</p>
											</view>
										</view>
									</uni-list-item>
								</template>								
							</expand-list>
						</view>		 
					</swiper-item>
				</template>			 
			</swiper>
		</view>
	</view>
</template>

<script>
	import ExpandList from '@/components/expand/expand-list.vue'
	import { getPendingTasks, getHandledTasks, getInitiateTasks } from '@/api/flow.js'
	
	export default {
		components: {
			ExpandList
		},
		data() {
			return {
				scrollLeft: 0,
				tabIndex: 0,
				tabBars: [
					{
						name: '待审批',
						id: '0'
					},
					{
						name: '已审批',
						id: '1'
					},
					{
						name: '我发起的',
						id: '2'
					}
				],
				searchText: '',
				params: {}
			}
		},
		onLoad(params) {
			this.params = params
			this.tabIndex = uni.getStorageSync("tabIndex").length === 0 ? 0 : uni.getStorageSync("tabIndex")
			this.$nextTick(()=>{
				this.loadData(this.params)
			})
		},
		methods: { 
			loadData(params) {
				let funcName
				if(this.tabIndex == 0){
					funcName = getPendingTasks
				}else if(this.tabIndex == 1){
					funcName = getHandledTasks
				}else if(this.tabIndex == 2){
					funcName = getInitiateTasks
				}
				
				const queryParams = []
				if(params.processName || params.processKey){
					if(this.tabIndex == 0){
						queryParams.push({
							key: "Q^temp.proc_def_key_^S",
							value: params.processKey
						})
					}else if(this.tabIndex == 1){
						queryParams.push({
							key: "Q^inst.proc_def_name_^SL",
							value: params.processName
						})
					}else if(this.tabIndex == 2){
						queryParams.push({
							key: "Q^proc_def_name_^SL",
							value: params.processName
						})
					}
				}
				if(params.startTime){
					if(this.tabIndex == 0){
						queryParams.push({
							key: "Q^temp.create_time_^DL",
							value: params.startTime
						})
					}else if(this.tabIndex == 1){
						queryParams.push({
							key: "Q^inst.create_time_^DL",
							value: params.startTime
						})
					}else if(this.tabIndex == 2){
						queryParams.push({
							key: "Q^create_time_^DL",
							value: params.startTime
						})
					}
				}
				if(params.endTime){
					if(this.tabIndex == 0){
						queryParams.push({
							key: "Q^temp.create_time_^DG",
							value: params.endTime
						})
					}else if(this.tabIndex == 1){
						queryParams.push({
							key: "Q^inst.create_time_^DG",
							value: params.endTime
						})
					}else if(this.tabIndex == 2){
						queryParams.push({
							key: "Q^create_time_^DG",
							value: params.endTime
						})
					}
				}
				
				this.$refs.expandlist[this.tabIndex].init(funcName, queryParams)
			},
			toInfo(item, index) {
				
			},
			changeTab(e) {
				let index = e.target.current
				this.tabIndex = index 
				uni.setStorageSync("tabIndex", this.tabIndex)
				this.params = {}
				this.loadData(this.params)
			},
			tapTab(e) {
				let tabIndex = e.target.dataset.current
				this.tabIndex = tabIndex
				uni.setStorageSync("tabIndex", this.tabIndex)
			},
			search1(e) {
				this.searchText = e.value
				this.queryData()
			},
			search2(e) {
				this.searchText = ''
				this.queryData()
			},
			queryData() {	  		
				const queryParams = []
				queryParams.push({
					key: "Q^temp.subject_^SL",
					value: this.searchText
				})
				this.$refs.expandlist[this.tabIndex].query(queryParams)
			}
		},
		onNavigationBarButtonTap(e) {
			uni.navigateTo({
				url: "/pages/flow/filter"
			})
		},
		onBackPress() {
			uni.setStorageSync("tabIndex", 0)
		}
	}
</script>

<style lang="scss">
	@import '@/themes/common-swiper.scss';
	@import '@/themes/common-list.scss';
	
</style>
